<template>
    <div class="container">
        <h3 style="margin-bottom: 20px">追溯查询</h3>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <div>
                <el-form-item label="查询方式：" prop="start">
                    <el-radio-group v-model="ruleForm.start" @change="handeStart">
                        <el-radio label="包装批号" border>包装批号</el-radio>
                        <el-radio label="追溯码" border>追溯码</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="包装批号：" prop="batch" v-if="ruleForm.start == '包装批号'">
                    <el-input placeholder="请输入内容" v-model.trim="ruleForm.batch" style="width: auto"></el-input>
                </el-form-item >
                <el-form-item label="追溯码：" prop="code" v-else>
                    <el-input placeholder="请输入内容" v-model.trim="ruleForm.code" style="width: auto"></el-input>
                </el-form-item>
            </div>
            <el-form-item class="footerFixed">
                <el-button type="primary" @click="submitRules('ruleForm')">查询</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import {search_getMsg,recall_findByBzBatch} from "@/request/api";
export default {
    components: {},
    data() {
        return {
            code: '',
            type: '',
            ruleForm: {
                start: '包装批号',
                batch: '',
                code: ''
            },
            rules: {
                start: [
                    { required: true, message: '不能为空', trigger: 'change' },
                ],
                batch: [
                    { required: true, message: '不能为空', trigger: 'change' },
                ],
                code: [
                    { required: true, message: '不能为空', trigger: 'blur' },
                ]
            }
        };
    },
    computed: {},
    methods: {
        init() {
        },
        handeStart(val) {
            if (val == '追溯码') {
                this.type = val
                this.code = this.ruleForm.code
            } else {
                this.type = val
                this.code = this.ruleForm.batch
            }
        },
        submitRules(formName){
            if (this.type == '') {
                this.type = '包装批号'
                this.code = this.ruleForm.batch
            } else {
                this.type = '追溯码'
                this.code = this.ruleForm.code
            }
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.type == '包装批号') {
                        recall_findByBzBatch({
                            batch: this.code
                        }).then((res) =>{
                            if (res.data) {
                                this.$router.push({
                                    path: '/traceBack/viewTrace',
                                    query: { batch: this.code }
                                });
                            }
                        })
                    } else if (this.type == '追溯码') {
                        search_getMsg(this.code).then((res) =>{
                            if (res.data) {
                                this.$router.push({
                                    path: '/traceBack/viewTrace',
                                    query: { code: this.code }
                                });
                            }
                        })
                    }
                } else {
                    return false;
                }
            })
        }
    },
    mounted () {
        this.init();
    },
	watch:{
		// searchName: function (val, oldVal) {
		// }
	},
};
</script>


<style scoped>
</style>
